from django.db import models
from django.contrib.auth.models import User
from books.models import Book


class Order(models.Model):
    """订单模型"""
    
    ORDER_STATUS_CHOICES = [
        ('pending', '待支付'),
        ('paid', '已支付'),
        ('confirmed', '已确认'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
        ('refunded', '已退款'),
    ]
    
    PAYMENT_METHOD_CHOICES = [
        ('alipay', '支付宝'),
        ('wechat', '微信支付'),
        ('offline', '线下支付'),
    ]
    
    id = models.BigAutoField(
        primary_key=True,
        verbose_name='编号',
        db_comment='订单ID'
    )
    order_no = models.CharField(
        max_length=32,
        unique=True,
        db_index=True,
        verbose_name='订单编号',
        db_comment='订单编号'
    )
    book = models.ForeignKey(
        Book,
        on_delete=models.PROTECT,
        related_name='orders',
        verbose_name='关联书籍',
        db_comment='关联书籍'
    )
    buyer = models.ForeignKey(
        User,
        on_delete=models.PROTECT,
        related_name='bought_orders',
        verbose_name='买家',
        db_comment='买家ID'
    )
    seller = models.ForeignKey(
        User,
        on_delete=models.PROTECT,
        related_name='sold_orders',
        verbose_name='卖家',
        db_comment='卖家ID'
    )
    
    # 交易信息
    price = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        verbose_name='交易价格',
        db_comment='交易价格'
    )
    status = models.CharField(
        max_length=20,
        choices=ORDER_STATUS_CHOICES,
        default='pending',
        db_index=True,
        verbose_name='订单状态',
        db_comment='订单状态'
    )
    payment_method = models.CharField(
        max_length=20,
        choices=PAYMENT_METHOD_CHOICES,
        null=True,
        blank=True,
        verbose_name='支付方式',
        db_comment='支付方式'
    )
    payment_time = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='支付时间',
        db_comment='支付时间'
    )
    
    # 交付信息
    pickup_location = models.CharField(
        max_length=200,
        verbose_name='取书地点',
        db_comment='取书地点'
    )
    pickup_time = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='取书时间',
        db_comment='取书时间'
    )
    notes = models.TextField(
        null=True,
        blank=True,
        verbose_name='订单备注',
        db_comment='订单备注'
    )
    
    # 时间字段
    created_at = models.DateTimeField(
        auto_now_add=True,
        db_index=True,
        verbose_name='创建时间',
        db_comment='创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True,
        verbose_name='更新时间',
        db_comment='更新时间'
    )
    completed_at = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='完成时间',
        db_comment='完成时间'
    )
    cancelled_at = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name='取消时间',
        db_comment='取消时间'
    )
    
    # AI聊天记录
    chat_summary = models.JSONField(
        null=True,
        blank=True,
        verbose_name='聊天摘要',
        db_comment='AI提取的关键信息'
    )
    
    class Meta:
        db_table = 'campus_order'
        db_table_comment = '订单表'
        verbose_name = '订单'
        verbose_name_plural = '订单'
        ordering = ['-created_at']
        indexes = [
            models.Index(fields=['buyer', 'status']),
            models.Index(fields=['seller', 'status']),
            models.Index(fields=['created_at', 'status']),
        ]
    
    def __str__(self):
        return f'{self.order_no} - {self.book.title}'


class OrderReview(models.Model):
    """订单评价"""
    
    RATING_CHOICES = [
        (1, '1星'),
        (2, '2星'),
        (3, '3星'),
        (4, '4星'),
        (5, '5星'),
    ]
    
    id = models.BigAutoField(
        primary_key=True,
        verbose_name='编号',
        db_comment='评价ID'
    )
    order = models.OneToOneField(
        Order,
        on_delete=models.CASCADE,
        related_name='review',
        verbose_name='关联订单',
        db_comment='关联订单'
    )
    reviewer = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        verbose_name='评价人',
        db_comment='评价人'
    )
    rating = models.IntegerField(
        choices=RATING_CHOICES,
        verbose_name='评分',
        db_comment='评分(1-5星)'
    )
    comment = models.TextField(
        null=True,
        blank=True,
        verbose_name='评价内容',
        db_comment='评价内容'
    )
    tags = models.JSONField(
        default=list,
        blank=True,
        verbose_name='评价标签',
        db_comment='评价标签'
    )
    created_at = models.DateTimeField(
        auto_now_add=True,
        verbose_name='评价时间',
        db_comment='评价时间'
    )
    
    class Meta:
        db_table = 'campus_order_review'
        db_table_comment = '订单评价表'
        verbose_name = '订单评价'
        verbose_name_plural = '订单评价'
        ordering = ['-created_at']
    
    def __str__(self):
        return f'{self.order.order_no} - {self.rating}星'
